package org.zjx.command.login;

import org.zjx.command.CompositeCommand;
import org.zjx.command.CompositeCommandBuilder;
import org.zjx.command.composite.FindImageCommand;
import org.zjx.core.IWindow;
import org.zjx.entity.Race;
import org.zjx.entity.ResolutionButton;
import org.zjx.state.StateManager;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.Map;
import java.util.Optional;

public class LoginCreateCommand extends CompositeCommand {
    public LoginCreateCommand(IWindow window, StateManager stateManager) {
        super(window, stateManager);
    }

    @Override
    protected void initializeSubCommands() {
        BufferedImage raceImage = getRaceImage();

        CompositeCommandBuilder builder = new CompositeCommandBuilder(targetWindow, stateManager);

        CompositeCommand serverFlow = builder
                .find(raceImage, LoginCommandConfig.Buttons.CREATE_CHOOSE_RACE_BUTTON, "figurePoint")
                .ifFound("figurePoint", this::chooseFigureFlow, this::chooseRaceFlow)
                .build();
        add(serverFlow);
    }

    private void chooseRaceFlow(CompositeCommandBuilder builder, Map<String, Object> context) {
        builder.move(46, 133).click().delay(1000);
    }

    private void chooseFigureFlow(CompositeCommandBuilder builder, Map<String, Object> context) {
        ResolutionButton resolutionButton = getRaceResolution();
        builder.find(resolutionButton.getPhoto(), resolutionButton.getRectangle(), "racePoint")
                .ifFound("racePoint", this::chooseName, (b, newContext) -> {
                    b.move(481, 545).click().delay(500);
                });
    }

    private void chooseName(CompositeCommandBuilder builder, Map<String, Object> context){
        Rectangle rect = LoginCommandConfig.Buttons.CREATE_NEXT_BUTTON;
        String username = stateManager.getAccountState().getUsername();

        for (int i = 0; i < 10; i++) {
            builder.key(KeyEvent.VK_BACK_SPACE).delay(100);
        }

        builder.write(username).delay(300);

        builder.move((int)rect.getCenterX(), (int)rect.getCenterY())
                .click()
                .delay(1000);
    }


    private BufferedImage getRaceImage() {
        Race race = stateManager.getAccountState().getRace();

        BufferedImage raceImage;
        if (race.getCategory() == 0) {
            raceImage = LoginCommandConfig.Images.CREATE_CHOOSE_RACE_REN_IMAGE;
        } else if (race.getCategory() == 1) {
            raceImage = LoginCommandConfig.Images.CREATE_CHOOSE_RACE_XIAN_IMAGE;
        } else {
            raceImage = LoginCommandConfig.Images.CREATE_CHOOSE_RACE_MO_IMAGE;
        }
        return raceImage;
    }

    private ResolutionButton getRaceResolution() {
        Race race = stateManager.getAccountState().getRace();
        Optional<ResolutionButton> resolutionButton = LoginCommandConfig.CREATE_RACE_BUTTONS.stream().filter(r -> r.getName().equals(race.getName())).findFirst();
        return resolutionButton.orElse(null);
    }

    public static boolean validate(IWindow window, BufferedImage screenshot) {
        FindImageCommand findImageCommand = new FindImageCommand(window, screenshot, LoginCommandConfig.Images.CREATE_PREVIOUS_IMAGE, LoginCommandConfig.Buttons.CREATE_PREVIOUS_BUTTON);
        findImageCommand.execute();
        return findImageCommand.findImage();
    }
}